package controllers;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;

import ontocompo.fractalcomponentmanager.api.ManagerItf;
import ontocompo.fractalcomponentmanager.api.UIElementItf;
import ontocompo.fractalcomponentmanager.api.WebApplicationItf;
import play.mvc.*;

public class Composition extends Controller {

    public static void loadApp() {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        HashMap<String, String> appList = new HashMap<String, String>();
        ArrayList<String> loadedAppList = new ArrayList<String>();
        
        if(ac != null) {
        	try {
        		appList = ac.getAvailableApps();
        		loadedAppList = ac.getLoadedAppStringList();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }

    	render(appList, loadedAppList);
    }
    
    public static void preloadApp(String appId) {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if(ac != null) {
        	try {
        		ac.loadAndInitApp(appId);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    	
    	loadApp();
    }

    public static void select() {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
    	
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        ArrayList<WebApplicationItf> loadedApps = new ArrayList<WebApplicationItf>();
        
        if(ac != null) {
        	try {
        		loadedApps = ac.getLoadedApp();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
    	render(loadedApps);
    }
    
    public static void selectAllUIElements(String appId) {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
    	
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if(ac != null) {
        	try {
        		ac.selectAllUIElements(appId);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        select();
    }
    
    public static void substitute() {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
    	
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        ArrayList<WebApplicationItf> loadedApps = new ArrayList<WebApplicationItf>();
        
        if(ac != null) {
        	try {
        		loadedApps = ac.getLoadedApp();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
    	render(loadedApps);
    }
    
    public static void selectForSubsitution(String elementId) {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
    	
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if(ac != null) {
        	try {
        		ac.selectForSubstitution(elementId);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    	
    	substitute();
    }
    
    public static void performSubstitution() {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
    	
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        ArrayList<WebApplicationItf> loadedApps = new ArrayList<WebApplicationItf>();
        if(ac != null) {
        	try {
        		loadedApps = ac.getLoadedApp();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
    	render(loadedApps);
    }
    
    public static void selectKeepElementForSubstitution(String elementId) {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
    	
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if(ac != null) {
        	try {
        		ac.performSubstitution(elementId);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        substitute();
    }

	public static void saveCompo(String appName) {
		ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
    	
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if(ac != null) {
        	try {
        		ac.saveCompo(appName);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
		render();
	}


}
